class Result<T> {
  message: string;
  status: number;
  data: T;
  check: boolean;
  constructor(data?: T, check?: boolean, status?: number, message?: string) {
    this.message = message || '操作成功';
    this.status = status;
    this.check = check;
    this.data = data;
  }

  success(data?: T, message?: string) {
    return new Result(data, null, 200, message);
  }

  error(message: string) {
    return new Result(null, null, 404, message);
  }
}

function CommonResult<T>(
  data?: T,
  check?: boolean,
  status?: number,
  message?: string,
) {
  return new Result(data, check, status, message);
}
type typeResult<T> = {
  data: T;
  check: boolean;
  status: number;
  message: string;
};

function validteRoles(req: any) {
  const user = req.user;
  if (user.id !== '22') {
    return CommonResult(null, false, 401, '权限不足');
  }
  return user;
}

export { CommonResult, typeResult, validteRoles };
